package com.sskj.qiangjin.ui.activity;


import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.qiangjin.R;
import com.sskj.qiangjin.component.DaggerUserDataComponent;
import com.sskj.qiangjin.presenter.SetPayPwdActivityPresenter;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

@Route(path = RConfig.APP_SET_PAY_PWD)//设置支付密码
public class SetPayPwdActivity extends BaseActivity<SetPayPwdActivityPresenter> {
    @BindView(R.id.tv_tel)
    TextView tvTel;
    @BindView(R.id.et_verification_code)
    EditText etVerificationCode;
    @BindView(R.id.tv_get_verification_code)
    TextView tvGetVerificationCode;
    @BindView(R.id.tv_pay_pwd)
    EditText tvPayPwd;
    @BindView(R.id.tv_pay_pwd_again)
    EditText tvPayPwdAgain;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;
    private DisposableSubscriber disposableSubscriber;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_set_pay_pwd;
    }

    @Override
    public SetPayPwdActivityPresenter getPresenter() {

        return new SetPayPwdActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.app_activity_set_pay_pwd));
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                tvTel.setText(userData.getTel());
                ClickUtil.click(tvGetVerificationCode, () -> mPresenter.sendCode(tvTel.getText().toString()));
            }
        });
        ClickUtil.click(btnConfirm, () -> {
            if (TextUtils.isEmpty(etVerificationCode.getText().toString())) {
                ToastUtil.showShort("请输入验证码");
                return;
            }
            if (TextUtils.isEmpty(tvPayPwd.getText().toString())) {
                ToastUtil.showShort("请输入密码");
                return;
            }
            if (!CheckUtil.isPayPwd(tvPayPwd.getText().toString()) || !CheckUtil.isPayPwd(tvPayPwdAgain.getText().toString())) {
                ToastUtil.showShort("密码格式错误");
                return;
            }
            if (!tvPayPwd.getText().toString().equals(tvPayPwdAgain.getText().toString())) {
                ToastUtil.showShort("两次密码输入不一致");
                return;
            }
            mPresenter.setPayPwd(tvPayPwd.getText().toString().trim()
                    , userData == null ? "" : userData.getAccount()
                    , etVerificationCode.getText().toString().trim()
                    , userData == null ? "" : userData.getTel());
        });
    }

    public void sendCodeOk() {
        startCount();
    }

    public void sendCodeError() {
    }

    public void startCount() {
        tvGetVerificationCode.setEnabled(false);
        DisposUtil.close(disposableSubscriber);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                tvGetVerificationCode.setText(String.format(App.INSTANCE.getString(R.string.app_count_down_register), time));
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                tvGetVerificationCode.setText("再次获取验证码");
                tvGetVerificationCode.setEnabled(true);
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }

            }
        };
        etVerificationCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == 6) {
                    btnConfirm.setEnabled(true);
                } else {
                    btnConfirm.setEnabled(false);
                }
            }
        });

        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);
    }

    public void setSuccess() {
        ARouter.getInstance().build(RConfig.APP_MAIN).navigation();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        DisposUtil.close(disposableSubscriber);
    }
}
